<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
include_once dirname(dirname(__FILE__)) . '/include/xmppChat.php';

class cronjobRequestAddress extends core {

    public function process($event_id, $access_token,$domain) {
        $messages_list = $this->db->get_results('SELECT raq.request_id, raq.request_message_id,
                                                        raq.guest_email,raq.guest_id,
                                                        raq.fb_guest_id,raq.fb_user_id,
                                                        ram.subject, ram.content, ram.event_id
	        				FROM request_address_queue raq,
                                                     request_address_messages ram   
	        				WHERE raq.request_message_id=ram.request_message_id
                                                AND checked=0 AND ram.event_id=' . $event_id . '
	        				ORDER BY request_id asc LIMIT 0,5000');
        if ($messages_list) {
            //Update checked field => tránh trường hợp đang gửi mail thì server lỗi hoặc đang gửi thì 1 threat khác chạy lại file làm 1 địa chỉ mail có thể được gửi nhiều lần
            $list_ids = '';
            foreach ($messages_list as $msg) {
                $list_ids .= $msg->request_id . ', ';
            }
            $list_ids = substr($list_ids, 0, -2);
            $this->db->query('UPDATE request_address_queue
                        SET checked=1
                        WHERE request_id IN (' . $list_ids . ')');
            
            //delete record after sent mail
            foreach ($messages_list as $msg) {
                $current_user =$this->db->get_row("SELECT first_name,last_name FROM users WHERE id= (SELECT user_id FROM events WHERE event_id=" . $msg->event_id.")");
                $user_name = $current_user->first_name." ".$current_user->last_name;
                if ($msg->guest_email != "") {
                    $data_event = $this->db->get_row("SELECT * FROM events WHERE event_id=" . $msg->event_id);
                    $user = $this->db->get_row("SELECT email FROM users WHERE id=" . $data_event->user_id);
                    $sent_by = $user->email;
                    //send email to guest
                    $encode_arg = base64_encode($msg->event_id . "-" . $msg->guest_id);
                    $messages = $msg->content . "\nPlease click <a href='".$domain."/reply_request_address.php?arg=" . $encode_arg . "'>here</a> to fill out your address.\nSent by: ".$user_name." (".$sent_by.")";
                    if (core::send_mail($msg->guest_email, $msg->subject, $messages)) {
                        $this->db->query('DELETE FROM request_address_queue
                        WHERE request_id = ' . $msg->request_id);
                    }
                    //Check complete sent email
                    $check_complete = $this->db->get_results('SELECT * FROM request_address_queue WHERE request_message_id=' . $msg->request_message_id . ' LIMIT 1');
                    if (empty($check_complete)) {//Đã xử lý hết trong queue
                        $this->db->query('UPDATE request_address_messages SET is_sent=1 where request_message_id=' . $msg->request_message_id);
                    }
                } else {
                     $data_event = $this->db->get_row("SELECT * FROM events WHERE event_id=" . $msg->event_id);
                    $user = $this->db->get_row("SELECT email FROM users WHERE id=" . $data_event->user_id);
                    $sent_by = $user->email;
                    //send message to guest by facebook chat
                    $encode_arg = base64_encode($msg->event_id . "-" . $msg->guest_id);
                    $messages = $msg->subject." \n".$msg->content . "\nPlease click link: ".$domain."/reply_request_address.php?arg=" . $encode_arg . " to fill out your address.\nSent by: ".$user_name." (".$sent_by.")";
                    
                    //$xmpp= new xmpp();
                    send_message_chat($access_token, $msg->fb_user_id, $msg->fb_guest_id, $messages);
                   
                    $this->db->query('DELETE FROM request_address_queue
                        WHERE request_id = ' . $msg->request_id);

                    //Check complete sent email
                    $check_complete = $this->db->get_results('SELECT * FROM request_address_queue WHERE request_message_id=' . $msg->request_message_id . ' LIMIT 1');
                    if (empty($check_complete)) {//Đã xử lý hết trong queue
                        $this->db->query('UPDATE request_address_messages SET is_sent=1 where request_message_id=' . $msg->request_message_id);
                    }
                }
            }
        }
    }

}
$event_id = $argv[1];
$access_token = $argv[2];
$domain = $argv[3];
 $processor = new cronjobRequestAddress();
$processor->process($event_id, $access_token,$domain);
echo "Done_email<br/>";
?>